// pages/classify/upload/index.js
const App = getApp()
var imageUtil=require('../../../utils/util.js');
Page({
  data:{
    text:'选择照片',
    tempFilePaths:'',
    loading_text:'',
    hidden:true,
    modalHidden:true,
  },
  onLoad:function(options){
    // 页面初始化 options为页面跳转所带来的参数
  },
  chooseimage:function(){
    var that=this
    wx.chooseImage({
      count: 1, // 最多可以选择的图片张数，默认9
      sizeType: ['original', 'compressed'], // original 原图，compressed 压缩图，默认二者都有
      sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
      success: function(res){
        that.setData({
          tempFilePaths:res.tempFilePaths,
          text:'重新上传'
        })
      },
    })
  },
  //保存订单
  saveOrder:function(){
    if(this.data.tempFilePaths==''){
      wx.showToast({
        title: '请先上传照片',
        icon: 'loading',
        duration: 4000
        })
    }else{
      var product_id=wx.getStorageSync('product')
      var decorate_id=wx.getStorageSync('decorate')
      var user_id=wx.getStorageSync('user_id')
       wx.showToast({
        title: '正在订单...',
        icon: 'loading',
        duration: 40000
        })
        var filePath=this.data.tempFilePaths[0]
      wx.uploadFile({
        url: 'https://www.sjwwljs.cn/index.php/api/Order/createOrder',
        filePath:filePath,
        name:'name',
        header: {
            'content-type':'multipart/form-data'
        },
         formData: {
           user_id:user_id,
           decorate_id:decorate_id,
           product_id:product_id
         }, // HTTP 请求中其他额外的 form data
        success: function(res){
          wx.hideToast()
        
            if(res.data=='true'){
               wx.removeStorageSync('product')
               wx.removeStorageSync('decorate')
                 wx.showToast({
                  title: '提交成功',
                  icon: 'success',
                  duration: 20000
                })
                setTimeout(function(){
                  wx.hideToast()
                 wx.switchTab({
                   url: '/pages/photo/index',
                 
                 })
                  },2000)
            }else{
              wx.showToast({
                  title: '提交失败',
                  icon: 'loading',
                  duration: 2000
                })
            }
          
        },
        fail: function() {
          // fail
          wx.hideToast()
           wx.showToast({
                  title: '提交失败',
                  icon: 'loading',
                  duration: 2000
                })
        },
        
      })
      
    }
  }
})